﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LiveProjectManager.DDD;


namespace LiveProjectManager.Controllers
{
    public class UserController : Controller
    {
        private static DDD.Services.UserService _service = new DDD.Services.UserService();


        //
        // GET: /User
        public ActionResult Index(int paginateBy = 0, int page = 1)
        {
            return Json(
                DDD.Services.UserService.toDict(_service.getAll(paginateBy, page)),
                JsonRequestBehavior.AllowGet
            );
        }


        //
        // GET: /User/Details/{Id}
        public ActionResult Details(long id)
		{
            DDD.Domains.User myUser = _service.getById(id);
			
			// Can't find a matching resource.
			if (myUser == null)
				return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);
			
			// Return formatted resource.
            return Json(myUser.toDict(), JsonRequestBehavior.AllowGet);
        }


        //
        // GET: /User/Details/{UserName}
        public ActionResult DetailsByName(string userName)
        {
            DDD.Domains.User myUser = _service.getByUserName(userName);

            // Can't find a matching resource.
            if (myUser == null)
                return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);

            // Return formatted resource.
            return Json(myUser.toDict(), JsonRequestBehavior.AllowGet);
        }


        //
        // POST: /User/Create
        [HttpPost]
        public ActionResult Create(FormCollection myFormData)
        {
            // If we can't create an user, one of the parameters must be invalid.
            if (!_service.create(myFormData["UserName"], myFormData["Email"], myFormData["Password"], myFormData["FirstName"], myFormData["LastName"]))
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
            return new HttpStatusCodeResult((int)HttpStatusCode.Created);
        }


        //
        // DELETE: /User/Delete
        [HttpDelete]
        public ActionResult Delete(FormCollection myFormCollection)
        {
            try
            {
                // Entity to delete is not found.
                if (!_service.exists(long.Parse(myFormCollection["Id"])))
                    return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);

                // Unknown error occured, unable to delete.
                if (!_service.remove(long.Parse(myFormCollection["Id"])))
                    return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);

                // Deletion successful
                return new HttpStatusCodeResult((int)HttpStatusCode.NoContent);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Invalid Id parameter (must be a long).");
            }
        }
    }
}
